﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net.Repository.Hierarchy;
using log4net;
using System.Reflection;

namespace CTP.Util
{
    /// <summary>
    /// Logging util API.
    /// </summary>
    public class LogUtil
    {
        public static bool LogToTrace = true;
        public static bool LogToLog4Net = true;
        public static bool LogToConsole = false;
        public static bool LogDebug = true;

        private static readonly ILog s_log4NetLog = LogManager.GetLogger("CTP");
        private static object s_memoryLogLock = new object();
        private static StringBuilder s_memoryLog = null;

        public static void Debug(string message)
        {
            if (LogDebug)
            {
                if (LogToTrace)
                    System.Diagnostics.Debug.WriteLine(message);
                if (LogToLog4Net)
                    s_log4NetLog.Debug(message);
                if (LogToConsole)
                    Console.WriteLine("debug: " + message);
                lock (s_memoryLogLock)
                {
                    if (s_memoryLog != null)
                    {
                        s_memoryLog.Append("debug: " + message + "\n");
                    }
                }
            }
        }

        public static void Info(string message)
        {
            if (LogToTrace)
                System.Diagnostics.Trace.TraceInformation(message);
            if (LogToLog4Net)
                s_log4NetLog.Info(message);
            if (LogToConsole)
                Console.WriteLine("info: " + message);
            lock (s_memoryLogLock)
            {
                if (s_memoryLog != null)
                {
                    s_memoryLog.Append("info: " + message + "\n");
                }
            }
        }

        public static void Warn(string message)
        {
            if (LogToTrace)
                System.Diagnostics.Trace.TraceWarning("Warning: " + message);
            if (LogToLog4Net)
                s_log4NetLog.Warn(message);
            if (LogToConsole)
                Console.WriteLine("warn: " + message);
            lock (s_memoryLogLock)
            {
                if (s_memoryLog != null)
                {
                    s_memoryLog.Append("warn: " + message + "\n");
                }
            }
        }

        public static void Error(string message)
        {
            if (LogToTrace)
                System.Diagnostics.Trace.TraceError(message);
            if (LogToLog4Net)
                s_log4NetLog.Error(message);
            if (LogToConsole)
                Console.Error.WriteLine("error: " + message);
            lock (s_memoryLogLock)
            {
                if (s_memoryLog != null)
                {
                    s_memoryLog.Append("error: " + message + "\n");
                }
            }
        }

        #region Memory Log Methods

        public static void EnableMemoryLog()
        {
            lock (s_memoryLogLock)
            {
                s_memoryLog = new StringBuilder();
            }
        }

        public static void DisableMemoryLog()
        {
            lock (s_memoryLogLock)
            {
                s_memoryLog = null;
            }
        }

        public static string GetMemoryLog()
        {
            lock (s_memoryLogLock)
            {
                if (s_memoryLog != null)
                {
                    return s_memoryLog.ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        #endregion
    
    }
}
